<?php
declare (strict_types=1);

namespace app\common\model\category;

use app\admin\logic\system\Config as SystemConfig;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 分类前台管理
 */
class Category extends BaseModel
{
    use softDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'category';

    protected $categoryType = 0;
    protected $hidden = ['create_time', 'update_time', 'delete_time'];

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['categoryType'];

    /**
     * 定义查询分类类型范围条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-10
     *
     * @param    [type]                         $query [description]
     * @return void [type]                                [description]
     */
    public function scopeCategoryType($query)
    {
        $type = $this->categoryType;
        // 是否自动检测
        if ($type != 0) {
            $config = SystemConfig::get('system.category', ['use_public' => 0], request()->mhm_id);
            if ($config['use_public'] == 1) {
                $type = 0;
            }
        }

        $query->where('category_type', $type);
    }

    /**
     * 设置当前分类类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-19
     */
    public function setCategoryType($category_type = 0): Category
    {
        // 全局配置使用课程分类
        $config = SystemConfig::get('system.category', ['use_public' => 0], request()->mhm_id);
        if ($config['use_public']) $category_type = 0;
        // 设置分类类型
        $this->categoryType = $category_type;

        return $this;
    }

    /**
     * 机构查询条件
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/03/11
     *
     * @param $query
     */
    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

}
